package battle;

import java.util.Random;

import objects.CombatEntity;

public class BattleFactory {

	private static BattleFactory battleFactory;
	
	public static BattleFactory getInstance() {
		if(battleFactory == null) {
			battleFactory = new BattleFactory();
		}
		return battleFactory;
	}
	
	private BattleFactory(){};
	
	
	public BattleResult calcBattle(CombatEntity attacker, CombatEntity defender) {
		
		Random random = new Random();
		
		int defenderHp = defender.getCurHp();
		BattleResultType battleResultType = null;
		int exp = 0;
		
		if(random.nextBoolean()) {
		if(defender.getDef() < attacker.getAtk()) {
			int dmg = attacker.getAtk() - defender.getDef();
			defenderHp -= dmg;
			System.out.println("Hit for "+dmg);
			battleResultType = BattleResultType.HIT;
			exp = 5;
		} else {
			battleResultType = BattleResultType.BLOCK;
		}
		} else {
			battleResultType = BattleResultType.MISS;
		}
		
		if(defenderHp <= 0) {
			exp = 50;
			battleResultType = BattleResultType.DEATH;
		}
		
		return new BattleResult(attacker.getCurHp(), defenderHp, battleResultType, exp);
	}
	
}
